package com.yneusoft.manage.aspect;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yneusoft.dao.entity.Department;
import com.yneusoft.dao.entity.DepartmentState;
import com.yneusoft.dao.mapper.DepartmentMapper;
import com.yneusoft.manage.model.param.SaveDepartmentParam;
import com.yneusoft.manage.model.param.UpdateDepartmentStateParam;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;


/**
 * 新增/修改时自动填充切面类
 */
@Aspect
@Component
@Slf4j
public class DepartmentHttpAspect {

    @Resource
    DepartmentMapper departmentMapper;


    @Around("execution(public * com.yneusoft.manage.controller.DepartmentManageController.saveDepartment(..))")
    public Object getDepartmentCode(ProceedingJoinPoint joinPoint) throws Throwable {
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyyMMdd");
        String s = sdf.format(localDateTime);
        Object[] args=joinPoint.getArgs();
        SaveDepartmentParam param= (SaveDepartmentParam) args[0];
        String s1="";
        if (param.getFatherAid()!=null){
            int fatherAid=param.getFatherAid();
            LambdaQueryWrapper<Department> wrap=new LambdaQueryWrapper<>();
            wrap.eq(Department::getAid,fatherAid);
            List<Department> list = departmentMapper.selectList(wrap);
            if (list.size() > 0) {
                List<Integer> list1=new ArrayList<>();
                for (Department department : list) {
                    list1.add(department.getLevel());
                }
                list1=new ArrayList<>(new LinkedHashSet<>(list1));
                s1 = String.format("%02d", (list1.get(0) + 1));
            }

        }else {
            s1=String.format("%02d",0);
        }
        LambdaQueryWrapper<Department> wrap1=new LambdaQueryWrapper<>();
        wrap1.likeRight(Department::getCode,s);
        Integer count = departmentMapper.selectCount(wrap1);
        String s2=String.format("%04d",(count + 1));
        String s3=(s+s1+s2);
        param.setDepartmentCode(s3);
        param.setStateName(DepartmentState.dataList.get(param.getId()).getName());
        return joinPoint.proceed(args);
    }

    @Before("execution(public * com.yneusoft.manage.controller.DepartmentManageController.isDepartment(..))")
    public void changeDepartmentState(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        UpdateDepartmentStateParam param= (UpdateDepartmentStateParam) args[0];
        LambdaQueryWrapper<Department> wrap=new LambdaQueryWrapper<>();
        wrap.eq(Department::getAid,param.getAid());
        Department department=departmentMapper.selectOne(wrap);
        DepartmentState departmentState;
        if (department.getStateId()==0){
            departmentState=DepartmentState.dataMap.get(1);
        }else {
            departmentState=DepartmentState.dataMap.get(0);
        }
        param.setStateId(departmentState.getId());
        param.setStateName(departmentState.getName());
    }
}
